package cn.sciento.starter.keyencrypt.core;


import java.util.Objects;
import cn.sciento.core.base.BaseConstants;
import cn.sciento.core.oauth.CustomUserDetails;
import cn.sciento.core.oauth.DetailsHelper;

public class EncryptContext {
  public static final String HEADER_ENCRYPT = "H-Request-Encrypt";
  
  private static volatile EncryptType defaultEncryptType = null;
  
  private static final ThreadLocal<EncryptType> encrypt = new ThreadLocal<>();
  
  public static EncryptType encryptType() {
    return (encrypt.get() == null) ? ((defaultEncryptType == null) ? EncryptType.DO_NOTHING : defaultEncryptType) : encrypt.get();
  }
  
  public static void setEncryptType(String encryptType) {
    for (EncryptType value : EncryptType.values()) {
      if (value.name().equalsIgnoreCase(encryptType)) {
        encrypt.set(value);
        break;
      } 
    } 
  }
  
  public static boolean isAllowedEncrypt() {
    CustomUserDetails userDetails = DetailsHelper.getUserDetails();
    return (userDetails == null || userDetails
      .getApiEncryptFlag() == null || BaseConstants.Flag.YES
      .equals(userDetails.getApiEncryptFlag()));
  }
  
  public static void clear() {
    encrypt.remove();
  }
  
  public static boolean isEncrypt() {
    return Objects.equals(encrypt.get(), EncryptType.ENCRYPT);
  }
  
  public static EncryptType getDefaultEncryptType() {
    return defaultEncryptType;
  }
  
  public static void setDefaultEncryptType(EncryptType defaultEncryptType) {
    EncryptContext.defaultEncryptType = defaultEncryptType;
  }
}
